package com.example.baselib.utils;

import com.example.baselib.utils.log.LogUtils;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Description:  TODO
 * Author: admin
 * CreateDate: 2021/4/6 9:40
 */
public class ThreadUtils {


    private static ThreadPoolExecutor executor;
    private static int cpu = Runtime.getRuntime().availableProcessors();

    static {
        executor = new ThreadPoolExecutor(cpu, cpu * 2 + 1, 2, TimeUnit.SECONDS, new LinkedBlockingDeque<>(5),
                new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable runnable) {
                        Thread thread = new Thread(runnable, "pool_code:" + runnable.hashCode());
                        return thread;
                    }
                }, new ThreadPoolExecutor.CallerRunsPolicy());
    }


    public static void execute(Runnable runnable) {
        if (executor != null) {
            executor.execute(runnable);
        }
    }


}
